package com.waiting.leetcode;

import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @Description
 * @Author bjweitingting
 * @Date 2019/3/22
 **/
public class AverageofLevelsInBinaryTree637 {

    public static void main(String[] args) {
        Queue<Integer> helper = new LinkedList<>();
        for (int i = 0; i < 10; i++) {
            helper.add(i);
        }
        System.out.println(helper.poll());
    }

    public List<Double> averageOfLevels(TreeNode root) {
        List<Double> result = new LinkedList<>();
        if (root != null) {
            Queue<TreeNode> helper = new LinkedList<>();
            helper.add(root);
            while (!helper.isEmpty()) {
                Double sum = 0.0;
                int count = helper.size();
                //遍历
                for (int i = 0; i < count; i++) {
                    TreeNode tmp = helper.poll();
                    sum += tmp.val;
                    if (tmp.left != null) {
                        helper.add(tmp.left);
                    }
                    if (tmp.right != null) {
                        helper.add(tmp.right);
                    }
                }
                //
                result.add(sum / count);
            }
        }
        return result;
    }
}
